<template>
	<view class="policy_box" v-if="data !== undefined && data!== null && data.length>0">
		<view class="policy_list" v-for="(item,index) in data" :key="index">
			<view class="policy_title_tag">
				<text class="policy_title">{{item.name}}</text>
			</view>
			<view class="ha_setup_box">
				<view class="ha_setup">
					<view class="ha_setup_list ha_setup_tit">
						<text class="average">政策</text>
						<text class="average">默认</text>
					</view>
					<view class="ha_setup_list" v-for="(list,nmb) in item.policy" :key="nmb">
						<text class="average">{{list.title}}</text>
						<view class="average" style="display: flex;justify-content: flex-end;">
							<u-switch v-model="list.default" asyncChange :activeValue='1' :inactiveValue='0'
								@change="Change($event,list.product_id,list.id)"></u-switch>
						</view>
					</view>
				</view>
			</view>
		</view>
	</view>
	<u-empty v-else mode="list" icon="/static/Empty/data_null.png">
	</u-empty>
</template>

<script>
	export default {
		data() {
			return {
				id: null,
				data: []
			}
		},
		onLoad(e) {
			this.id = e.id
			this._request()
		},
		methods: {
			Change(e, product_id, id) {
				if (e) {
					let _this = this
					uni.showModal({
						content: '确定要设置为默认吗',
						success: (res) => {
							if (res.confirm) {
								_this.modify(product_id, id)
							}
						}
					})
				}
			},
			async _request() {
				uni.showLoading({
					title: '加载中'
				});
				const userinfo = uni.getStorageSync('userinfo');
				const {
					data: res
				} = await this.$myHttp({
					url: 'setting/getBelowPolicy',
					data: {
						user_id: this.id
					},
					header: {
						'token': userinfo.token
					},
				});
				if (res.code == 1) {
					this.data = res.data
					uni.hideLoading();
				} else {
					uni.showToast({
						title: res.msg,
						icon: 'none',
						duration: 3000
					})
				}
			},
			async modify(product_id, id) {
				uni.showLoading({
					title: '加载中'
				});
				let that = this
				const userinfo = uni.getStorageSync('userinfo');
				const {
					data: res
				} = await this.$myHttp({
					url: 'setting/setBelowPolicy',
					data: {
						user_id: this.id,
						relation_id: id,
						product_id: product_id
					},
					header: {
						'token': userinfo.token
					},
				});
				if (res.code == 1) {
					uni.showToast({
						title: res.msg,
						icon: 'none',
						duration: 3000,
						success() {
							setTimeout(() => {
								that._request()
							}, 1500)
						}
					})
					uni.hideLoading();
				} else {
					uni.showToast({
						title: res.msg,
						icon: 'none',
						duration: 3000
					})
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.policy_box {
		padding: 30rpx;
		box-sizing: border-box;

		.policy_list {
			padding: 20rpx;
			border-radius: 15rpx;
			box-shadow: 0px 1px 14px -10px #999;
			margin-bottom: 30rpx;

			.policy_title_tag {
				padding: 0 0 20rpx 0;
				margin-bottom: 20rpx;
				border-bottom: #ebebeb dotted 1px;

				.policy_title {
					border-left: 8rpx solid #3c9cff;
					padding: 0 0 4rpx 10rpx;
					width: 100%;
					display: block;
					font-size: 28rpx;
					color: #333;
					font-weight: bold;
				}
			}
		}
	}

	.ha_setup_box {

		.ha_setup {
			.ha_setup_list {
				margin-bottom: 20rpx;
				display: flex;
				align-items: center;
				justify-content: space-between;

				.average {
					display: block;
					text-align: end;
					font-size: 24rpx;
					// flex: 1;
				}
			}

			.ha_setup_tit {
				border-bottom: #eee 1px solid;
				padding-bottom: 20rpx;
			}
		}
	}
</style>